package com.hrz.datasync.controller;

import com.hrz.datasync.service.JobAndTriggerService;
import com.hrz.datasync.utils.PageUtil;
import com.hrz.datasync.utils.ResponseEntity;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @作 者: 侯建军
 * @日 期: 2018/11/06 16:34:56
 * @描 述: 任务控制器
 */
@Controller
@RequestMapping("/")
@Log
public class JobController extends BaseController {

    @Autowired
    private JobAndTriggerService jobAndTriggerService;

    @RequestMapping("/index")
    public String index()  {
        log.info("主页面");
        return "/index";
    }
    @RequestMapping("/job")
    public String job()  {
        log.info("任务页");
        return "/page/job";
    }
    @RequestMapping("/logs")
    public String log()  {
        log.info("日导页");
        return "/page/logs";
    }
    @RequestMapping(value = "/datagrid", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String, Object>> queryjob(HttpServletRequest request, HttpServletResponse response) {
//        log.info("queryjob");
//        setHeader(response);
//        search.setParams(getParams(request));
        return jobAndTriggerService.getPageJob(1,10);
    }
    /**
     * @param jobClassName   类名
     * @param jobGroupName   组名
     * @param cronExpression 表达式，如：0/5 * * * * ? (每隔5秒)
     * @Title: addJob
     * @Description: (添加Job)
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity addJob(
            @RequestParam(value = "jobClassName") String jobClassName,
            @RequestParam(value = "jobGroupName") String jobGroupName,
            @RequestParam(value = "cronExpression") String cronExpression,
            HttpServletResponse response) {
        try {
            setHeader(response);
            jobAndTriggerService.addJob(jobClassName, jobGroupName, cronExpression);
            return ResponseEntity.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.FAILURE(e.getMessage());
        }
    }

    /**
     * 功能描述:
     * 暂停Job
     *
     * @参 数:
     * @返 回:
     * @作 者: 侯建军
     * @日 期: 2018/11/06 05:10:21
     */
    @RequestMapping(value = "/pause", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity pauseJob(
            @RequestParam(value = "jobClassName") String jobClassName,
            @RequestParam(value = "jobGroupName") String jobGroupName,
            HttpServletResponse response) {
        try {
            setHeader(response);
            jobAndTriggerService.pauseJob(jobClassName, jobGroupName);
            return ResponseEntity.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.FAILURE(e.getMessage());
        }
    }

    /**
     * 功能描述:
     * 恢复Job
     *
     * @参 数:
     * @返 回:
     * @作 者: 侯建军
     * @日 期: 2018/11/06 05:10:06
     */
    @RequestMapping(value = "/resume", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity resumeJob(
            @RequestParam(value = "jobClassName") String jobClassName,
            @RequestParam(value = "jobGroupName") String jobGroupName,
            HttpServletResponse response) {
        try {
            setHeader(response);
            jobAndTriggerService.resumejob(jobClassName, jobGroupName);
            return ResponseEntity.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.FAILURE(e.getMessage());
        }
    }

    /**
     * 功能描述:
     * 重新设置Job
     *
     * @参 数:
     * @返 回:
     * @作 者: 侯建军
     * @日 期: 2018/11/06 05:09:57
     */
    @RequestMapping(value = "/reschedule", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity rescheduleJob(
            @RequestParam(value = "jobClassName") String jobClassName,
            @RequestParam(value = "jobGroupName") String jobGroupName,
            @RequestParam(value = "cronExpression") String cronExpression,
            HttpServletResponse response) {
        try {
            setHeader(response);
            jobAndTriggerService.updateJob(jobClassName, jobGroupName, cronExpression);
            return ResponseEntity.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.FAILURE(e.getMessage());
        }
    }
    /**
     * 功能描述:
     * 删除Job
     *
     * @参 数:
     * @返 回:
     * @作 者: 侯建军
     * @日 期: 2018/11/06 05:09:42
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity deleteJob(@RequestParam(value = "jobClassName") String jobClassName, @RequestParam(value = "jobGroupName") String jobGroupName,
                             HttpServletResponse response) {
        try {
            setHeader(response);
            jobAndTriggerService.deleteJob(jobClassName, jobGroupName);
            return ResponseEntity.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.FAILURE(e.getMessage());
        }
    }
}
